﻿using System.Linq;
using Nxg.Blog.Core.Commands.Posts;
using Nxg.Blog.Core.Documents;

namespace Nxg.Blog.Core.ViewProjections.Admin {
    public class BlogPostEditBindingModel {
        public int PostId { get; set; }
    }

    public class BlogPostEditViewModel {
        public BlogPost BlogPost { get; set; }
    }

    public class BlogPostEditViewProjection : IViewProjection<BlogPostEditBindingModel, NewPostCommand> {
        private readonly DataContext _db;

        public BlogPostEditViewProjection (DataContext db) {
            _db = db;
        }

        public NewPostCommand Project (BlogPostEditBindingModel input) {
            var post = _db.Post.SingleOrDefault (x => x.Id == input.PostId);
            return new NewPostCommand {
                Title = post.Title,
                    TitleSlug = post.TitleSlug,
                    MarkDown = post.MarkDown,
                    Tags = post.Tags,
                    PubDate = post.PubDate,
                    Published = post.IsPublished,
                    Id = post.Id,
                    Content = post.Content
            };
        }
    }
}